<?php
class Home extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('users');
		$this->load->model('user_routine_time');
		$this->load->model('cal_model');
		$this->load->model('medicines');
	}
	
	function index($error = NULL){
		$data['error'] = $error;
		$this->load->view('home/home_view', $data);
	}
	
	function login(){
		if($this->session->userdata('logged_in')){
			redirect(base_url().'index.php/home/', 'refresh');
		}
		$action = FALSE;
		$action = $this->input->post('action');
		if(!$action){
			$this->load->view('home/login_page');
		}
		if($action == 'do_login'){
			//echo "Log me in";
			
			$data = array(
				'email' => $this->input->post('email'),
				'password' =>$this->input->post('pass'),
				'logged_in' => TRUE
			); 
			$user = $this->users->validate_user_elderly($data);
			if($user){
				$data['fname'] = $user['fname'];
				$data['lname'] = $user['lname'];
				$data['user_id'] = $user['user_id'];
				$this->session->set_userdata($data);
				redirect('index.php/profile', 'refresh');
			}
			else{
				$care = $this->users->validate_user_care($data);
				if($care){
					$elder_id = $this->users->get_elder_id($care['care_id']);
					if($elder_id){
						$data['fname'] = $care['fname'];
						$data['lname'] = $care['lname'];
						$data['care_id'] = $care['care_id'];
						$data['user_id'] = $elder_id;
						$this->session->set_userdata($data);
						redirect('index.php/profile', 'refresh');
					}
				}
				else{
					$this->index('Incorrect email or password');
				}
				
			}
		}
		
	}
	
	function logout(){
		$this->session->sess_destroy();
		
		redirect(base_url(), 'refresh');
	}
	
	function test(){
		$ch = 0;
		$uid = $this->session->userdata('user_id');
		$check_time = date("H:i").":00";
		$check_time_10 = date("H:i", strtotime($check_time."-3 minutes")).":00";
		$check_time_20 = date("H:i", strtotime($check_time."-6 minutes")).":00";
		$query = $this->medicines->get_notify_medicine($uid,$check_time);
		$query2 = $this->medicines->get_notify_medicine($uid,$check_time_10);
		$query3 = $this->medicines->get_notify_medicine($uid,$check_time_20);
		if($query){
			$med['medicines'] = $query;
			$med['notify'] = 1;
			foreach($query as $q){
				$his = array(
					'user_id' => $uid,
					'med_id' => $q->med_id,
					'time_id' => $q->time_id,
					'count' => 0,
					'date' => date("Y-m-d")
				);
				
				$add = $this->medicines->add_history($his);
			}
			echo json_encode($med);
		}
		elseif($query2){
			$med['medicines'] = $query2;
			$med['notify'] = 2;
			echo json_encode($med);
		}
		elseif($query3){
			$med['medicines'] = $query2;
			$med['notify'] = 3;
			
			foreach($query3 as $q){
				$his_check = array(
					'user_id' => $uid,
					'med_id' => $q->med_id,
					'time_id' => $q->time_id,
					'count' => 1,
					'date' => date("Y-m-d")
				);
				$check = $this->medicines->get_history($his_check);
				if($check){
					$ch = 1;
					break;
				}
			}
			if($ch == 1){
				echo 0;
			}
			else{
				echo json_encode($med);
			}
			
		}
		else{
			echo 0;
		}
		/*foreach($query->result() as $row){
			$time = substr($row->notify_time,0,5);
			$time_10min = date("H:i",strtotime($time."+10 minutes"));
			$time_20min = date("H:i",strtotime($time."+20 minutes"));
			if($time == date("H:i")){
				echo 1;
			}
			if($time_10min == date("H:i")){
				echo 2;
			}
			if($time_20min == date("H:i")){
				echo 3;
			}
			// else {
				// echo $time;
			// }
		}*/
		// $time = "22:46:00";
		// if($time == date("H:i:s")){
			// echo 1;
		// }
		// else{
			// echo date("H:i:s");
		// }
		
	}
	function pro(){
		$this->load->view('profile/profile_view');
	}
	
	function signUp(){
		$this->load->view('home/signup');
	}
	function regis_user(){
		$user_info = array(
			'email' => $this->input->post('email'),
			'password' => $this->input->post('password'),
			'firstname' => $this->input->post('fname'),
			'lastname' => $this->input->post('lname'),
			'gender' => $this->input->post('sex'),
			'bday' => $this->input->post('b-day')
		);
		$insert = $this->users->insert_new_user($user_info);
		if($insert){
			if($this->input->post('breakfast')){
				$breakfast_time = array(
					'user_id' => $insert,
					'time_id' => 1,
					'time' => $this->input->post('breakfast')
				);
			}
			if($this->input->post('lunch')){
				$lunch_time = array(
					'user_id' => $insert,
					'time_id' => 2,
					'time' => $this->input->post('lunch')
				);
			}
			if($this->input->post('dinner')){
				$dinner_time = array(
					'user_id' => $insert,
					'time_id' => 3,
					'time' => $this->input->post('dinner')
				);
			}
			if($this->input->post('bedtime')){
				$bed_time = array(
					'user_id' => $insert,
					'time_id' => 4,
					'time' => $this->input->post('bedtime')
				);
			}
			$routine[0] = $breakfast_time;
			$routine[1] = $lunch_time;
			$routine[2] = $dinner_time;
			$routine[3] = $bed_time;
			
			for($i=0; $i<4; $i++){
				$insert_time = $this->user_routine_time->insert_routine_time($routine[$i]);
			}
			$cal = array('user_id' => $insert);
			$insert_cal = $this->cal_model->create_cal($cal);
			
			if($this->input->post('care_email')){
				$email = $this->input->post('care_email');
				$pass = $this->password_generate();
				$care_info = array(
					'firstname' => $this->input->post('care_fname'),
					'lastname' => $this->input->post('care_lname'),
					'email' => $email,
					'password' => $pass
				);
				
				$insert_care = $this->users->insert_new_care($care_info);
				$elca = array('elderly_id'=>$insert, 'care_id'=>$insert_care);
				$elder_care = $this->users->insert_elder_care($elca);
				$this->send_password($email, $pass);
				
			}
			
		}
		else{
			echo FALSE;
		}
	}
	
	function send_password($email, $password){
		$config = array(
			'protocol' => 'smtp',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_user' => 'victoria.pong@gmail.com',
			'smtp_pass' => 'ac22092532'
		);
		
		$this->load->library('email', $config);
		
		$this->email->set_newline("\r\n");
		
		$this->email->from('victoria.pong@gmail.com', 'Victoria');
		$this->email->to($email);
		$this->email->subject('EWMN ANNOUNCE');
		$msg = "Your elderly has given access to their profile.\n Your password is ".$password;
		$this->email->message($msg);
		
		if($this->email->send()){
			echo 1;
		}
		else{
			echo 0;
		}

	}
	
	function password_generate(){
		$length = 5;
		$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
		$string = "";    
		for ($p = 0; $p < $length; $p++) {
			$string .= $characters[mt_rand(0, strlen($characters)-1)];
		}
		return $string;
	}
}